﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using eu.h8me.Parsing.ParserLib;

namespace eu.h8me.Parsing.LexerLib2 {

    /// <summary>
    /// Class used to store and reference a regular expression, and handle matching of a regular expression.
    /// </summary>
    public sealed class RegEx {

        /// <summary>
        /// Event called back when a regular expression matches.
        /// </summary>
        /// <param name="state">Current state of the lexer.</param>
        /// <param name="data">Data that was matched by the regular expression.</param>
        /// <returns>Token that is matched by this regex or null in case this is not a match.</returns>
        public delegate Token MatchEventType(object state, string[] data);

        // Data.
        private string _RegExString;
        private TNFA _Automaton;
        private object _Tag;
        private int? _Precedence;
        private event MatchEventType _MatchEvent;

        /// <summary>
        /// Initialize a new regular expression from the passed string regex, using the named regular expression set that is
        /// passed.
        /// </summary>
        /// <param name="regex">Regular expression to parse.</param>
        /// <param name="namedre">Set of currently defined named regular expressions.</param>
        internal RegEx(string regex, Dictionary<string, TNFA> namedre) {
            // Parse the passed regular expression.
            _RegExString = regex;
            _Automaton = RegExParser.Parse(regex, namedre);
        }

        /// <summary>
        /// Initialize a new regular expression with the passed automaton.
        /// </summary>
        /// <param name="tnfa">Automaton to bind to regular expression.</param>
        internal RegEx(TNFA tnfa) {
            // Store automaton.
            _Automaton = tnfa;
        }

        /// <summary>
        /// Access the automaton that is bound to this TNFA.
        /// </summary>
        internal TNFA Automaton {
            get {
                // Return bound automaton.
                return _Automaton;
            }
        }

    }

}
